<?php

function load_img($path) {
  switch (take_ext(take_file_name($path))) {
    case 'gif'  : return imagecreatefromgif ($path);
    case 'jpe'  : 
    case 'jpg'  : 
    case 'jpeg' : return imagecreatefromjpeg($path);
    case 'png'  : return imagecreatefrompng ($path);
    case 'wbmp' : return imagecreatefromwbmp($path);
    case 'xbm'  : return imagecreatefromxbm ($path);
    case 'xpm'  : return imagecreatefromxpm ($path);
    case 'gd'   : return imagecreatefromgd  ($path);
    case 'gd2'  : return imagecreatefromgd2 ($path);
    default     : return false;
  }
}
  
  function take_file_name($f_path) {
    $changed = strtr($f_path, '\\', '/');
    $tab = explode('/', $changed);
    $last = count($tab)-1;
    return($tab[$last]);
  }
  
  function take_ext($file_name) {
    $ext = explode('.', $file_name);
    $last = count($ext)-1;
    if($last == 0) {$last = 1;}
    return($ext[$last]);
  }

function empty_image($text = 'Image Generating Error ...') {
  $img = imagecreatetruecolor(400, 100);
  $color = imagecolorallocate($img, 255, 255, 255);
  imagefill($img, 1, 1, $color);
  $color = imagecolorallocate($img, 192, 192, 192);
  imagestring($img, 5, 81, 46, $text, $color);
  $color = imagecolorallocate($img, 0, 0, 0);
  imagestring($img, 5, 80, 45, $text, $color);
  imagerectangle($img, 0, 0, 399, 99, $color);
  imagepng($img);
  imagedestroy($img);
  die('');
}

function check_parameters_db_src($dat) {
  if($dat['fields'] < 1)
    return false;
  
  $last_font = '';
  
  for($i=1; $i<=$dat['fields']; $i++) {
    if(($dat['f'.$i.'_x'] == null) || ($dat['f'.$i.'_y'] == null) || ($dat['f'.$i.'_s'] == null) || ($dat['f'.$i.'_c'] == null) || (!ereg('^[a-fA-F0-9]{6}$', $dat['f'.$i.'_c'])))
      return false;
    
    if($dat['f'.$i.'_f'] != $last_font) { 
      $sql_ret = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`="'.$dat['f'.$i.'_f'].'" AND `type`="FONT" LIMIT 1');
      $sql_dat = @mysql_fetch_assoc($sql_ret);
      if($sql_dat['id'] == null)
        return false;
      $last_font = $dat['f'.$i.'_f'];
    }
  }
  return true;
}

function check_parameters_db_data($dat) {
  if($dat['f_used'] < 1)
    return false;
    
  $last_font = '';
  
  for($i=1; $i<=$dat['f_used']; $i++) {
    $check_tab = explode('|', $dat['f'.$i]);
    
    if(($check_tab[0] < 0 || $check_tab[0] > count(get_values(PATCH))) || (!ereg('^[a-fA-F0-9]{6}$', $check_tab[3])) || ($check_tab[4] != 0 && $check_tab[4] != 1))
      return false;
    
    if($check_tab[4] == 1)
      if((!ereg('^[a-fA-F0-9]{6}$', $check_tab[5])) || ($check_tab[6] == null) || ($check_tab[7] == null))
        return false;
    
    if($check_tab[2] != $last_font) {
      $sql_ret = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`="'.intval($check_tab[2]).'" AND `type`="FONT" LIMIT 1');
      $sql_dat = @mysql_fetch_assoc($sql_ret);
      if($sql_dat['id'] == null)
        return false;
      $last_font = $check_tab[2];
    }
  }
  return true;
}

function check_parameters_arr($dat) {
  if($dat[0][1] < 1)
    return false;
    
  $last_font = '';
  
  for($i=1; $i<=$dat[0][1]; $i++) {
    if(($dat[$i][1] == null) || (!ereg('^[a-fA-F0-9]{6}$', $dat[$i][2])) || ($dat[$i][3] == null) || (!ereg('^[a-fA-F0-9]{6}$', $dat[$i][4])) || ($dat[$i][5] == null) || ($dat[$i][6] == null))
      return false;
    
    if($dat[$i][1] != $last_font) {
      $sql_ret = @mysql_query('SELECT * FROM `'.TAB_SRC.'` WHERE `id`="'.intval($dat[$i][1]).'" AND `type`="FONT" LIMIT 1');
      $sql_dat = @mysql_fetch_assoc($sql_ret);
      if($sql_dat['id'] == null)
        return false;
      $last_font = $dat[$i][1];
    }
  }
  return true;
}

function puttext($img, $x, $y, $size, $hexcolor, $fontpath, $text) {
  $makedcolor = imagecolorallocate($img, hexdec(substr($hexcolor, 0, 2)), hexdec(substr($hexcolor, 2, 2)), hexdec(substr($hexcolor, 4, 2)));
  @imagettftext($img, $size, 0, $x, $y+$size, $makedcolor, $fontpath, $text) or empty_image();
  return true;
}

function puttextshadow($img, $x, $y, $sx, $sy, $size, $hexcolor, $hexshadowcolor, $fontpath, $text) {
  $makedcolor = imagecolorallocate($img, hexdec(substr($hexshadowcolor, 0, 2)), hexdec(substr($hexshadowcolor, 2, 2)), hexdec(substr($hexshadowcolor, 4, 2)));
  @imagettftext($img, $size, 0, $x+$sx, $y+$sy+$size, $makedcolor, $fontpath, $text) or empty_image();
  
  $makedcolor = imagecolorallocate($img, hexdec(substr($hexcolor, 0, 2)), hexdec(substr($hexcolor, 2, 2)), hexdec(substr($hexcolor, 4, 2)));
  @imagettftext($img, $size, 0, $x, $y+$size, $makedcolor, $fontpath, $text) or empty_image();
  return true;
}

function draw_preview($prev_img, $data, $text_num=9) {
  if($text_num > $data['fields'])
    $put = $data['fields'];
  elseif($text_num <= 0)
    $put = 1;
  else
    $put = $text_num;
  
  for($i=1; $i<=$put; $i++) {
    if($data['f'.$i.'_f'] != $lastfont) {
      $ret = @mysql_query('SELECT `id`,`path` FROM `'.TAB_SRC.'` WHERE `id`="'.$data['f'.$i.'_f'].'" AND `type`="FONT" LIMIT 1');
      $dat = @mysql_fetch_assoc($ret);
      $lastfont = $dat['id'];
    }
    
    if($i == 1)
      puttextshadow($prev_img, $data['f'.$i.'_x'], $data['f'.$i.'_y'], 1, 1, $data['f'.$i.'_s'], $data['f'.$i.'_c'], '808080', './../'.$dat['path'], 'Character Name');
    else {
      $str = 'Text '.$i;
      puttext($prev_img, $data['f'.$i.'_x'], $data['f'.$i.'_y'], $data['f'.$i.'_s'], $data['f'.$i.'_c'], './../'.$dat['path'], $str);
    }
  }
  return true;
}

?>